package com.example.demo;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JSON_Duixiang {

    private static ObjectMapper objectmapper = new ObjectMapper();

    public static void main(String[] args) throws JsonProcessingException {

        Person person = new Person();
        person.setAge(30);
        person.setName("张三");
        person.setPassword("123456");

        //对象转 json 字符串
        String s = objectmapper.writeValueAsString(person);
        System.out.println(s);

        //json 字符串转对象
        String json = "{\"name\":\"张三\",\"password\":\"123456\",\"age\":15}";
        Person person1 = objectmapper.readValue(json,Person.class);
        System.out.println(person1);
    }
}
